//
//  InvestmentBriefView.m
//  SP2P
//
//  Created by 吴建东 on 16/6/3.
//  Copyright © 2016年 EIMS. All rights reserved.
//

#import "InvestmentBriefView.h"
#import "InvestmentBriefCell.h"

@interface InvestmentBriefView ()<UITableViewDelegate,UITableViewDataSource>
{
    UIView *_headerView;
    UIView *_footerView;
    NSMutableArray *_titleDataArray;
    NSMutableArray *_detailDataArray;
    NSMutableArray *_colorDataArray;
    
    CGFloat _headerHeight;
    CGFloat _footerHeight;
    
    UILabel     *_briefLabel;
    UIView      *_briefView;
    UILabel     *_directionValue;
    UIImageView *_directionImageView;
}
@end

@implementation InvestmentBriefView

- (id)initWithFrame:(CGRect)frame type:(NSInteger)viewType {
    if (self = [super initWithFrame:frame]) {
        _viewType = viewType;
        [self initData];
        [self initView];
    }
    return self;
}

- (void)transButtonClick {
    if (self.deleagate && [self.deleagate respondsToSelector:@selector(transButtonClick)]) {
        [self.deleagate transButtonClick];
    }
}

- (void)initData {
    _titleDataArray = [NSMutableArray array];
    _detailDataArray = [NSMutableArray array];
    _colorDataArray = [NSMutableArray array];
    
    [_titleDataArray addObjectsFromArray:@[@"1.资产管理计划",@"2.信托计划",@"3.短期票据",@"4.货币基金",@"5.保险类"]];
    [_detailDataArray addObjectsFromArray:@[@"必须是国有金融机构发行或作为监督托管方的固定收益类资产管理计划，如证券公司 基金子公司等金融机构。",@"获得中国银监会批准，持有新的金融牌照的信托公司发行的信托计划。",@"包括银行本票、银行承兑汇票、商业本票、商业承兑汇票及到期日在一年之内的各种债券（如政府公债、公司债、金融债券等）。",@"专门投向风险小的货币市场工具，区别于其他类型的开放式基金，具有高安全性、高流动性、稳定收益性，具有“准储蓄”的特征。",@"由保险公司承保的各类资产，包括保付代理、融资租赁等。"]];
    [_colorDataArray addObjectsFromArray:@[[ColorTools colorWithHexString:@"#FF8C21"],[ColorTools colorWithHexString:@"#58DEBD"],[ColorTools colorWithHexString:@"#499BE4"],[ColorTools colorWithHexString:@"#A46CEB"],[ColorTools colorWithHexString:@"#38B337"]]];
}

- (void)setDescriptionStr:(NSString *)descriptionStr {
    _descriptionStr = descriptionStr;
    
    NSString *str = _descriptionStr;
//    str = [str stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"\t" withString:@""];
//    str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];

    _briefLabel.text = str;
    
    CGFloat briefHeight = [MyTools getSizeOfString:str maxWidth:MSWIDTH - 40 maxHeight:1000 withFontSize:12].height;
    _briefLabel.frame = RECTMAKE(10, KAdaptNum(5), GetWidth(_briefView) - 20, briefHeight);
    _briefView.frame = RECTMAKE(10, KAdaptNum(10), MSWIDTH - 20, briefHeight + KAdaptNum(10));
    _directionValue.frame = RECTMAKE(10, GetMaxY(_briefView) + KAdaptNum(15), MSWIDTH - 20, KAdaptNum(20));
    _directionImageView.frame = RECTMAKE(0, GetMaxY(_directionValue) + KAdaptNum(15), MSWIDTH, KAdaptNum(180));
    _headerHeight = GetMaxY(_directionImageView) + KAdaptNum(10);
    
    _tableView.tableHeaderView = _headerView;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_titleDataArray.count) {
        static NSString *cellID = @"cellID";
        InvestmentBriefCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"InvestmentBriefCell" owner:self options:nil][0];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.circleVIew.backgroundColor = _colorDataArray[indexPath.row];
        cell.titleLabel.text = _titleDataArray[indexPath.row];
        CGFloat contentHeight = [MyTools getSizeOfString:_detailDataArray[indexPath.row] maxWidth:MSWIDTH - 58 maxHeight:1000 withFontSize:12].height + 5;
        cell.detailLabel.frame = RECTMAKE(38, KAdaptNum(40), MSWIDTH - 58, contentHeight);
        cell.detailLabel.text = _detailDataArray[indexPath.row];
        
        cell.frame=CGRectMake(0, 0, MSWIDTH, GetMaxY(cell.detailLabel) + KAdaptNum(10));
        
        return cell;
    }else {
        EmptyTableViewCell *cell = [[EmptyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"emptyCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.backgroundColor = KbackgroundColor;
        
        cell.frame=CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, _tableView.frame.size.height);
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 100.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1.0f;
}

#pragma mark - View
- (void)initView {
    _tableView = [UITableView tableViewWithFrmae:RECTMAKE(0, 0, self.frame.size.width, self.frame.size.height) backgroundColor:[UIColor whiteColor] delegate:self dataSource:self tableViewStyle:UITableViewStyleGrouped separatorStyle:UITableViewCellSeparatorStyleSingleLine superview:self];
    _tableView.bounces = NO;
    
    _tableView.tableHeaderView = [self loadHeaderView];
    _tableView.tableFooterView = [self loadFooterView];
}

- (UIView *)loadHeaderView {
    _headerView = [UIView viewWithFrame:CGRectZero backgroundColor:[UIColor whiteColor] superview:nil];
    
    NSString *briefStr;
    if (self.viewType == 4) {
        briefStr = @"通过‘随取宝’所投资资产均为最安全的资产类型，同时为了防止单一资产发生系统性风险，将严格遵循一键分散原则。";
    }else {
        briefStr = @"季季增是由随取宝提供资产管理与咨询的三个月到期自动退出的理财工具。该计划所对应的资金均100%适用于随取宝网站理财本金保障计划，安全性高。您出借所获得的收益可选择复投或提取，并由系统为您实现分散投资，更好的满足您多样化的理财需求。";
    }
    CGFloat briefHeight = [MyTools getSizeOfString:briefStr maxWidth:MSWIDTH - 40 maxHeight:1000 withFontSize:12].height;
    
    _briefView = [UIView viewWithFrame:RECTMAKE(10, KAdaptNum(10), MSWIDTH - 20, briefHeight + KAdaptNum(20)) backgroundColor:[ColorTools colorWithHexString:@"#FFF1E8"] superview:_headerView];
    _briefView.layer.cornerRadius = KAdaptNum(10);
    if (self.viewType == 4) {
        _briefView.backgroundColor = SETCOLOR(250, 233, 197, 1.0);
    }
    
    _briefLabel = [UILabel labelWithFrame:RECTMAKE(10, KAdaptNum(10), GetWidth(_briefView) - 20, briefHeight) text:briefStr textColor:KColor textFont:12 fitWidth:NO superview:_briefView];
    _briefLabel.numberOfLines = 0;
    
    _directionValue = [UILabel labelWithFrame:RECTMAKE(10, GetMaxY(_briefView) + KAdaptNum(15), MSWIDTH - 20, KAdaptNum(20)) text:@"资产投向为：" textColor:KBlackColor textFont:12 fitWidth:NO superview:_headerView];
    
    _directionImageView = [UIImageView imageViewWithFrame:RECTMAKE(0, GetMaxY(_directionValue) + KAdaptNum(15), MSWIDTH, KAdaptNum(180)) imageFile:@"investment_brief_direction" superview:_headerView];
    
    _headerHeight = GetMaxY(_directionImageView);
    _headerView.frame = RECTMAKE(0, 0, MSWIDTH, _headerHeight);
    
    return _headerView;
}

- (UIView *)loadFooterView {
    _footerView = [UIView viewWithFrame:CGRectZero backgroundColor:[UIColor whiteColor] superview:nil];
    
    if (self.viewType == 4) {
        NSString *briefStr = @"经过上述资产投向的资产保证后，随取宝还分别设立了多层级保证金制度，保证资产兑付风险。";
        CGFloat briefHeight = [MyTools getSizeOfString:briefStr maxWidth:MSWIDTH - 20 maxHeight:1000 withFontSize:12].height;
        
        UILabel *briefLabel = [UILabel labelWithFrame:RECTMAKE(10, KAdaptNum(10), MSWIDTH - 20, briefHeight) text:briefStr textColor:KGrayColor textFont:12 fitWidth:NO superview:_footerView];
        briefLabel.numberOfLines = 0;
        
        ClickButton *transButton = [[ClickButton alloc] initWithFrame:RECTMAKE(15, GetMaxY(briefLabel) + KAdaptNum(35), MSWIDTH - 30, KAdaptNum(44))];
        [transButton setTitle:@"立即转入" forState:UIControlStateNormal];
        [transButton addTarget:self action:@selector(transButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:transButton];
        
        _footerView.frame = RECTMAKE(0, 0, MSWIDTH, GetMaxY(transButton) + KAdaptNum(10));

    }else {
        NSString *briefStr = @"证券固定收益类资管 信托产品 银行短期债等固定收益类 产品形成的投资组合，分散投资，收益稳定。";
        CGFloat briefHeight = [MyTools getSizeOfString:briefStr maxWidth:MSWIDTH - 20 maxHeight:1000 withFontSize:12].height;
        
        UILabel *briefLabel = [UILabel labelWithFrame:RECTMAKE(10, KAdaptNum(10), MSWIDTH - 20, briefHeight) text:briefStr textColor:KBlackColor textFont:12 fitWidth:NO superview:_footerView];
        briefLabel.numberOfLines = 0;
        
        UIImageView *profitImageView = [UIImageView imageViewWithFrame:RECTMAKE(0, GetMaxY(briefLabel) + KAdaptNum(20), MSWIDTH, KAdaptNum(222)) imageFile:@"" superview:_footerView];
        if (self.viewType == 1) {
            profitImageView.image = [UIImage imageNamed:@"WechatIMG463"];
        }else if (self.viewType == 2) {
            profitImageView.image = [UIImage imageNamed:@"WechatIMG463"];
        }else {
            profitImageView.image = [UIImage imageNamed:@"WechatIMG463"];
        }
        
        _footerView.frame = RECTMAKE(0, 0, MSWIDTH, GetMaxY(profitImageView) + KAdaptNum(10));
    }
    
    
    return _footerView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
